import { defineStore } from 'pinia'
import { exerciseApi } from '@/api'

export const useExerciseStore = defineStore('exercise', {
  state: () => ({
    exerciseRecords: [],
    currentRecord: null,
    loading: false
  }),
  
  getters: {
    // 获取最近的运动记录
    recentRecords: (state) => {
      return state.exerciseRecords.slice(0, 10)
    },
    
    // 获取本周运动总时长
    weeklyExerciseMinutes: (state) => {
      const now = new Date()
      const weekStart = new Date(now.setDate(now.getDate() - now.getDay()))
      weekStart.setHours(0, 0, 0, 0)
      
      return state.exerciseRecords
        .filter(record => {
          // 使用正确的字段名 exerciseDate 而不是 recordDate
          const recordDate = record.exerciseDate || record.recordDate;
          return new Date(recordDate) >= weekStart
        })
        .reduce((total, record) => {
          return total + (record.duration || 0)
        }, 0)
    }
  },
  
  actions: {
    // 创建运动记录
    async createRecord(recordData) {
      this.loading = true
      try {
        const response = await exerciseApi.createRecord(recordData)
        this.exerciseRecords.unshift(response)
        this.currentRecord = response
        return response
      } catch (error) {
        console.error('创建运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 更新运动记录
    async updateRecord(recordId, recordData) {
      this.loading = true
      try {
        const response = await exerciseApi.updateRecord(recordId, recordData)
        // 更新本地状态
        const index = this.exerciseRecords.findIndex(record => record.exerciseId === recordId)
        if (index !== -1) {
          this.exerciseRecords[index] = response
        }
        if (this.currentRecord && this.currentRecord.exerciseId === recordId) {
          this.currentRecord = response
        }
        return response
      } catch (error) {
        console.error('更新运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取某日期的运动记录
    async getRecords(date) {
      this.loading = true
      try {
        const response = await exerciseApi.getRecords(date)
        this.exerciseRecords = response
        return response
      } catch (error) {
        console.error('获取运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取日期范围内的运动记录
    async getRecordsByDateRange(startDate, endDate) {
      this.loading = true
      try {
        const response = await exerciseApi.getRecordsByDateRange(startDate, endDate)
        this.exerciseRecords = response
        return response
      } catch (error) {
        console.error('获取运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 获取最近的运动记录
    async getRecentRecords(limit = 10) {
      this.loading = true
      try {
        const response = await exerciseApi.getRecentRecords(limit)
        this.exerciseRecords = response
        return response
      } catch (error) {
        console.error('获取运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 删除运动记录
    async deleteRecord(recordId) {
      this.loading = true
      try {
        await exerciseApi.deleteRecord(recordId)
        // 从本地状态中移除
        this.exerciseRecords = this.exerciseRecords.filter(record => record.exerciseId !== recordId)
        if (this.currentRecord && this.currentRecord.exerciseId === recordId) {
          this.currentRecord = null
        }
      } catch (error) {
        console.error('删除运动记录失败', error)
        throw error
      } finally {
        this.loading = false
      }
    },
    
    // 清空当前记录
    clearCurrentRecord() {
      this.currentRecord = null
    }
  }
})